package com.xiaoming.backend.util;

import com.xiaoming.backend.dto.request.RegisterRequest;
import com.xiaoming.backend.dto.response.UserResponse;
import com.xiaoming.backend.entity.db.po.CarTaskPO;
import com.xiaoming.backend.entity.db.po.OrderPO;
import com.xiaoming.backend.entity.db.po.UserPO;
import com.xiaoming.backend.entity.db.po.WastePO;
import com.xiaoming.backend.entity.orm.Order;
import com.xiaoming.backend.entity.orm.User;

import java.util.Date;

public class EntityUtil {
    public static UserPO registerRequestTOUserPO(RegisterRequest request) {
        if (request == null){
            return null;
        }
        UserPO user = new UserPO();
        user.setUserName(request.getUserName());
        user.setPassWard(request.getPassWard());
        user.setName(request.getName());
        user.setRole(request.getRole());
        user.setAvatar(request.getAvatar());
        user.setFacilityId(request.getFacilityId());
        return user;
    }

    public static User userPOTOUser(UserPO userPO) {
        if (userPO == null){
            return null;
        }
        User user = new User();
        user.setUserId(userPO.getUserId());
        user.setUserName(userPO.getUserName());
        user.setName(userPO.getName());
        user.setRole(userPO.getRole());
        user.setFacilityId(userPO.getFacilityId());
        user.setAvatar(userPO.getAvatar());
        user.setCrtdDate(userPO.getCrtdDate());
        return user;
    }

    public static User userPOTOJwtUser(UserPO userPO) {
        if (userPO == null){
            return null;
        }
        User user = new User();
        user.setUserId(userPO.getUserId());
        user.setUserName(userPO.getUserName());
        user.setPassWard(userPO.getPassWard());
        user.setName(userPO.getName());
        user.setRole(userPO.getRole());
        user.setFacilityId(userPO.getFacilityId());
        user.setAvatar(userPO.getAvatar());
        user.setCrtdDate(userPO.getCrtdDate());
        return user;
    }

    public static UserResponse userTOUserResponse(User user) {
        if (user == null){
            return null;
        }
        UserResponse userResponse = new UserResponse();
        userResponse.setUserID(user.getUserId());
        userResponse.setUserName(user.getUserName());
        userResponse.setName(user.getName());
        userResponse.setRole(user.getRole());
        userResponse.setRoleName(user.getRoleName());
        userResponse.setFacilityId(user.getFacilityId());
        userResponse.setFacilityName(user.getFacilityName());
        return userResponse;
    }

    public static OrderPO order2OrderPO(Order order) {
        if (order == null){
            return null;
        }
        OrderPO orderPO = new OrderPO();
        // 基础订单信息
        orderPO.setOrderId(order.getOrderId());
        orderPO.setOrderSqno(order.getOrderSqno());
        orderPO.setOrderWasteId(order.getOrderWasteId());

        // 设施关联信息
        orderPO.setOrderStartFacilityId(order.getOrderStartFacilityId());
        orderPO.setOrderEndFacilityId(order.getOrderEndFacilityId());

        // 状态信息
        orderPO.setOrderStatus(order.getOrderStatus());

        // 处置信息（操作人+时间+备注）
        orderPO.setDisposqlUserId(order.getDisposqlUserId());
        orderPO.setDisposqlDate(order.getDisposqlDate());
        orderPO.setDisposqlRemark(order.getDisposqlRemark());

        // 创建信息（创建人+时间+备注）
        orderPO.setCrtdUserId(order.getCrtdUserId());
        orderPO.setCrtdDate(order.getCrtdDate());
        orderPO.setCrtdRemark(order.getCrtdRemark());

        return orderPO;
    }

    public static WastePO order2WastePO(Order order) {
        if (order == null){
            return null;
        }
        WastePO wastePO = new WastePO();
//        wastePO.setWasteId(order.getOrderWasteId());
        wastePO.setWasteType(order.getOrderWasteType());
        wastePO.setWeight(order.getWasteWeight());
        wastePO.setDisposalMethod(order.getDisposalMethod());
        return wastePO;
    }

    public static CarTaskPO order2CarTaskPO(Order order) {
        if (order == null){
            return null;
        }
        CarTaskPO carTaskPO = new CarTaskPO();
        carTaskPO.setOrderId(Long.valueOf(order.getOrderId()));
        carTaskPO.setTaskSqno(getTaskSqnoStr());
        carTaskPO.setOrderId(Long.valueOf(order.getOrderId()));
        carTaskPO.setRoute(order.getOrderStartFacilityName());
        carTaskPO.setTaskStatus("待运输");
        return carTaskPO;
    }

    // 生成订单号
    public static String getOrderSqnoStr(){
        return "OR" + BackendDealUtils.formatOrderSqno(new Date());
    }

    // 生成运输订单号
    public static String getTaskSqnoStr(){
        return "TK" + BackendDealUtils.formatOrderSqno(new Date());
    }

}
